<template>
  <view class="comBox">
    <sickTitleCom></sickTitleCom>
    <view class="body">
      <view>
        <view class="name titleBold">{{ info.name }}</view>
        <view v-if="info.tips" class="tips"> {{ info.tips }} </view>
        <view class="sickList">
          <view
            v-for="item in itemData"
            :key="item.id"
            :class="[info.rowBlock === true ? 'sickBox blocksInl' : 'sickBox']"
          >
            <view :class="[item.active ? 'sickSpan cur' : 'sickSpan']" @click="choseList(item)">
              {{ item.name }}
              <span v-if="item.active" class="curImg"><img :src="curIcon" /></span>
            </view>
            <view v-if="item.tips" class="kidTips">{{ item.tips }}</view>
          </view>
        </view>
        <view v-if="checkValue === otherNum" class="inputdisplay">
          <!-- <view><span class="name">其他</span></view> -->
          <view class="displayInput">
            <textarea
              v-model="mainData.exerciseModeOther"
              class="uni-textarea"
              maxlength="20"
              placeholder-class="phcolor"
            />
          </view>
          <view class="clear"></view>
        </view>
      </view>
      <!-- 下一题上一题按钮 -->
      <nextProBtnCom @nextBtn="nextBtn" @proBtn="proBtn"></nextProBtnCom>
    </view>
  </view>
</template>

<script>
import nextProBtnCom from '@/components/nextProButton';
import sickTitleCom from './sickTitleCom';
import curIcon from '@/static/icon/cur.png';
export default {
  components: {
    // 上一题下一题组件
    nextProBtnCom,
    sickTitleCom
  },
  props: {
    // 是否展示搜索按钮
    info: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      checkValue: undefined,
      curIcon: curIcon,
      noChose: false,
      mainData: {
        exerciseModeOther: ''
      },
      itemData: [],
      otherNum: ''
    };
  },
  computed: {
    active() {
      return this.$store.getters.active;
    },
    assessForm() {
      return this.$store.getters.assessForm;
    }
  },
  mounted() {
    let data = JSON.parse(JSON.stringify(this.info.options));
    data.map((item) => {
      item.active = false;
      return item;
    });
    this.itemData = data;
    this.otherNum = data[data.length - 1].id;
  },
  methods: {
    nextBtn() {
      // 疾病直接提交接口
      // 必填校验
      let check = this.itemData.some((item) => item.active);
      if (this.info.require && !check) {
        uni.showToast({ title: `请选择${this.info.name}`, icon: 'none' });
        return false;
      }
      let subData = [];
      this.itemData.forEach((item) => {
        if (item.active) {
          subData.push(item.id);
        }
      });
      if (this.checkValue !== this.otherNum) {
        this.mainData.exerciseModeOther = '';
      }
      if (this.checkValue === this.otherNum && !this.mainData.exerciseModeOther) {
        uni.showToast({ title: `请输入其他运动方式`, icon: 'none' });
        return false;
      }
      let submitData = this.assessForm;
      let curActive = this.active + 1;
      submitData[this.info.key] = subData;
      submitData.exerciseModeOther = this.mainData.exerciseModeOther || '';
      // 数组求和，因为后端不支持数组
      let numbers = submitData[this.info.key];
      submitData[this.info.key] = numbers.reduce(function (sum, number) {
        // sum2 前两个数的和
        return sum + number;
      }, 0);
      this.$store.commit('assessQuestion/SET_ASSESSFORM', submitData);
      this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
    },
    proBtn() {
      let curActive = this.active - 1;
      this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
    },
    choseList(val) {
      this.checkValue = '';
      const indexs = this.itemData.findIndex((item) => item.id === val.id);
      this.itemData[indexs].active = !this.itemData[indexs].active;
      let otherCheck = this.itemData.some((item) => item.active && item.id === this.otherNum);
      if (otherCheck) {
        this.checkValue = this.otherNum;
      }
    },
    inputChange(e) {}
  }
};
</script>
<style lang="scss">
@import './../style/radioInput.scss';
</style>
